package com.zhou.blog.controller;

import com.zhou.blog.utils.QiniuUtils;
import com.zhou.blog.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @title:UploadController
 * @Author Zhou
 * @Date:2022/5/8 18:28
 * @Version 1.0
 */
@RestController
@RequestMapping("api/upload")
public class UploadController {
    @PostMapping
    public Result upload(@RequestParam("image")MultipartFile file){
        //获取原文件名称
        String originalFilename = file.getOriginalFilename();
        //唯一的文件名
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        //上传到七牛云，降低应用服务器带宽消耗
        boolean upload = QiniuUtils.upload(file, fileName);
        if (upload) {
            return Result.success(QiniuUtils.url + fileName);
        }
        return Result.fail(5555,"上传失败");

    }

}
